#include <iostream>
#include <vector>

using namespace std;

class Solution {
public:
    long long countSubarrays(vector<int>& nums, int k) {
        int t = -1;
        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] > t)
                t = nums[i];
        }

        for (int i = 0; i < nums.size(); ++i) {
            if (nums[i] == t)
                nums[i] = 1;
            else
                nums[i] = 0;
        }

        int sum = 0;
        int cnt = 0;
        int n = nums.size();
        for (int i = 0, j = 0; j < n;) {
            sum += nums[j++];
            while (sum == k) {
                cnt += n - j + 1;
                sum -= nums[i++];
            }
        }
        return cnt;
    }
};

int main()
{
    vector<int> nums = { 1,3,2,3,3 };
    int k = 2;

    cout << Solution().countSubarrays(nums, k);


    return 0;
}